package com.naiterui.ehp.bs.pharmacist.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.pharmacist.entity.Pharmacist;
import com.naiterui.ehp.bs.pharmacist.entity.PharmacistStandardDict;
import com.naiterui.ehp.bs.pharmacist.exception.ExceptionCodes;
import com.naiterui.ehp.bs.pharmacist.feign.PharmacistEsignFeignClient;
import com.naiterui.ehp.bs.pharmacist.repository.mysql.PharmacistRepository;
import com.naiterui.ehp.bs.pharmacist.repository.mysql.StandardDictRepository;
import com.naiterui.ehp.bs.pharmacist.service.IUserService;
import com.naiterui.ehp.bs.pharmacist.utils.Constants;
import com.naiterui.ehp.bs.pharmacist.vo.user.UserAuthDetailVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.UserDetailInfoVO;
import com.naiterui.ehp.bs.pharmacist.vo.user.UserInfoVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description 用户业务服务
 * @Author gongbaoqiang
 * @Date 2019/12/6 8:18 PM
 */
@Slf4j
@Service
public class PharmacistUserSericeImpl implements IUserService {

    @Autowired
    private PharmacistRepository pharmacistRepository;

    @Autowired
    private StandardDictRepository dictRepository;

    @Autowired
    private PharmacistEsignFeignClient pharmacistEsignFeignClient;
    @Value("${ehp.domain.img}")
    private String imgDomain;

    @Override
    public UserInfoVO getUserInfo(Long pharmacistId) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (pharmacist == null) {
            throw new BusinessException(ExceptionCodes.USER_NOT_EXIST);
        }
        UserInfoVO vo = new UserInfoVO();
        BeanUtil.copyProperties(pharmacist, vo);
        vo.setPhone(StringUtil.securePhone(pharmacist.getPhone()));
        return vo;
    }

    @Override
    public UserDetailInfoVO getUserDetail(Long pharmacistId) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (pharmacist == null) {
            throw new BusinessException(ExceptionCodes.USER_NOT_EXIST);
        }
        UserDetailInfoVO vo = new UserDetailInfoVO();
        BeanUtil.copyProperties(pharmacist, vo);
        vo.setSex(pharmacist.getGenderStr());
        vo.setCardNo(StringUtil.secureIdcard(pharmacist.getCardNo()));

        PharmacistStandardDict dict = dictRepository.findFirstByCode(pharmacist.getEthnicity());
        //民族
        vo.setEthnicity(dict == null ? "" : dict.getName());
        vo.setLicenseUrl(Constants.EHP_DOMAIN_IMG + pharmacist.getLicenseUrl());

        //身份证图片地址
        if (StringUtils.isNotBlank(pharmacist.getFrontUrl())) {
            vo.setFrontUrl(Constants.EHP_DOMAIN_IMG + pharmacist.getFrontUrl());
        }

        if (StringUtils.isNotBlank(pharmacist.getBackUrl())) {
            vo.setBackUrl(Constants.EHP_DOMAIN_IMG + pharmacist.getBackUrl());
        }

        return vo;
    }

    @Override
    public void getRealnameCode(Long pharmacistId) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (pharmacist == null) {
            throw new BusinessException(ExceptionCodes.PRESCRIPTION_SIGN_DOCTOR_NOT_RECORD_ERROR);
        }
        pharmacistEsignFeignClient.getRealnameCode(pharmacist.getName(), pharmacist.getPhone(), pharmacist.getCardNo());
    }

    @Override
    public void checkRealnameCode(Long pharmacistId, String code) throws BusinessException {
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        //实名验证，实名状态由签名服务维护
        pharmacistEsignFeignClient.checkRealnameByCode(pharmacist.getPhone(), code);
        Constants.delRdsAuthUuidKey(pharmacistId);
    }

    @Override
    public UserAuthDetailVO getUserAuthDetail(Long pharmacistId, String uuid) throws BusinessException {
        String authUuid = Constants.getRdsAuthUuid(pharmacistId);
        if (StringUtils.isBlank(authUuid)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR, "错误请求");
        }
        if (!uuid.equals(authUuid)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR, "错误请求");
        }
        Pharmacist pharmacist = pharmacistRepository.get(pharmacistId);
        if (pharmacist == null) {
            throw new BusinessException(ExceptionCodes.USER_NOT_EXIST);
        }
        UserAuthDetailVO vo = new UserAuthDetailVO();
        BeanUtil.copyProperties(pharmacist, vo);
        if (StringUtils.isNotBlank(vo.getFrontUrl())) {
            vo.setFrontUrl(imgDomain + vo.getFrontUrl());
        }
        if (StringUtils.isNotBlank(vo.getBackUrl())) {
            vo.setBackUrl(imgDomain + vo.getBackUrl());
        }
        return vo;
    }

}
